package com.example.administrator.nurse.present;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.example.administrator.nurse.MainActivity;
import com.example.administrator.nurse.R;
import com.example.administrator.nurse.base.BaseActivity;
import com.example.administrator.nurse.common;
import com.example.administrator.nurse.util.passwordHelper;
import com.example.administrator.nurse.widget.LoginInputEdit;
import com.loopj.android.http.RequestParams;

/**
 * Created by Administrator on 2016/4/22.
 *
 */

public class LoginActivity extends BaseActivity {

    private LoginInputEdit account;
    private LoginInputEdit password;
    private boolean isRemeberPw = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        common.config(this, getWindowManager().getDefaultDisplay().getWidth());
        //startLoadingDialog();
        if(common.getSp(null).getString("isLogin","-1").trim().equals("yes")){
            common.initUserCache(null);
            startOtherActivity(LoginActivity.this, MainActivity.class);
        }else {
            initView();
        }
    }

    @Override
    protected void onFailure(int requestCode, Object object, String errorMsg) {
        Log.d("zzzzz", errorMsg);
        stopLoadingDialog();
    }

    @Override
    protected void onSuccess(int requestCode, Object object, String response) {
        Log.d("zzzzz", response);
        stopLoadingDialog();
        switch (requestCode){
            case Login:

                if(response.trim().equals("0")){
                    t("登录失败,密码错误!");
                }else if(response.trim().equals("2")){
                    t("登录失败,账号不存在!");
                }else {
                    String[] temp = response.trim().split("_");
                    if(temp[0].equals("1")) {
                        try {
                            t("登录成功!");
                            common.nId = temp[1];
                            common.nName = temp[2];
                            common.nIdOfHospital = (temp[3] == null ? "null" : temp[3]);
                            common.getSp(null).edit().putString("nId",      common.nId).commit();
                            common.getSp(null).edit().putString("nName",    common.nName).commit();
                            common.getSp(null).edit().putString("nIdOfHospital", common.nIdOfHospital).commit();
                            if(temp.length>=5){
                                //common.nHeadUrl = common.Host +"/"+ (temp[4] == null ? "" : temp[4]);
                                common.nHeadUrl = "http://115.159.2.72/"+(temp[4] == null ? "" : temp[4]);
                                Log.d("LoginActivity","地址："+common.nHeadUrl);
                                common.getSp(null).edit().putString("nHeadUrl", common.nHeadUrl).commit();
                            }
                        }catch (Exception ignored){}
                        common.isLogin = isRemeberPw;
                        if (common.isLogin) {
                            common.getSp(null).edit().putString("isLogin", "yes").commit();
                        }
                        common.getSp(null).edit().putString("nAccount", account.getStringText()).commit();
                        common.getSp(null).edit().putString("nPw", passwordHelper.MD5(password.getStringText())).commit();
                        startOtherActivity(LoginActivity.this, MainActivity.class);
                    }
                }
                break;
        }
    }

    private final int Login = 0x11;

    private void initView(){
        configCommonHeader(LoginActivity.this, getResources().getString(R.string.login_title), 0);
        f(R.id.forget_pass_txt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startOtherActivity(LoginActivity.this, ForgetPassActivity.class);
            }
        });
         // 14e1b600b1fd579f47433b88e8d85291
         // e10adc3949ba59abbe56e057f20f883e

        //暂时添加的游客按钮12.12
        /*f(R.id.tourist1_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startOtherActivity(LoginActivity.this, MainActivity.class);
            }
        });*/

        f(R.id.login_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (account.isOk("账号不能少于3个字")) {
                    if (password.isOk("密码不能少于3个字")) {
                        startLoadingDialog();
                        RequestParams requestParams = new RequestParams();
                        requestParams.put("account",account.getStringText());
                        //requestParams.put("password",passwordHelper.MD5(password.getStringText()));//暂时不加密
                        //doPost(Login, null, common.Host + "/Controller/funcController.php?func=login",requestParams);
                        requestParams.put("password",password.getStringText());
                        doPost(Login, null, "http://115.159.2.72/index.php",requestParams);
                    }
                }
            }
        });
        account  = (LoginInputEdit) f(R.id.account);
        account.numberLimit = 3;
        password = (LoginInputEdit) f(R.id.password);
        password.numberLimit = 3;
        ((CheckBox)f(R.id.checkbox_btn)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                isRemeberPw = isChecked;
            }
        });
    }

    @Override
    protected int setLayotId() {
        return R.layout.login_layout;
    }


}
